package log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Classe criada para manipular os log da aplicação 
 * Os logs são salvos em formato HTML;
 * Salva tudo na pasta My Games padrao windows
 * @author Adriano
 */
public class HandlerChangeHtml {
    private static FileHandler fileHandler;
    private static Formatter formatter;

    /**Salvar o log da aplicação em HTML*/
    
    public void salve() throws SecurityException, IOException {
        File file = new File(System.getProperty("user.home") + "\\Documents\\My Games\\rotulador\\log");
        if (!file.exists()) {
            file.mkdirs();
        }
        Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
        logger.setLevel(Level.FINER);
        SimpleDateFormat format = new SimpleDateFormat("dd_MM_YYYY_hhmmss");
        fileHandler = new FileHandler(System.getProperty("user.home") + "\\Documents\\My Games\\rotulador\\log\\" + format.format(new Date()) + "_log.html");
        formatter = new HtmlFormateLog();
        fileHandler.setFormatter(formatter);
        logger.addHandler(fileHandler);
        createFileStyle();
    }

    public static FileHandler getFileHandler() {
        return fileHandler;
    }

    /**Método que cria o estilo do log em html caso o arquvio não foi criado*/
    private void createFileStyle() throws IOException {
        File dir = new File(System.getProperty("user.home") + "\\Documents\\My Games\\rotulador\\log");
        File arq = new File(dir, "estilo.css");
        
        try {
            if(!arq.exists()){
                arq.createNewFile();
                FileWriter fileWriter = new FileWriter(arq, false);
                PrintWriter printWriter = new PrintWriter(fileWriter);
                printWriter.println("body{background-color:#eee}.SEVERE{color:red}.WARNING{color:#e5dd00}.INFO{color:blue}.CONFIG{color:#aaa}.FINER{color:#2bc4a9}.FINE{color:#8e1b6f}.FINEST{color:green}table{font-size:15px}td{padding:2px;border-bottom: #000 solid 2px;}");
                printWriter.flush();
                printWriter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
